package com.dyzs.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.dyzs.util.Constants;
import com.dyzs.util.HttpDownloader;
import com.dyzs.util.Pager;
import com.dyzs.util.ReadProperties;
import com.dyzs.util.ToolUtils;




public class CommonListActivity extends ListActivity{
	
	protected Pager pager=new Pager();	
	protected TextView emptyTV;	
	protected GridView gv;
	
	
	
	
	
	
	public void onException(Context context,final Exception e) {
		((Activity) context).runOnUiThread(new Runnable() {
			public void run() {
				if (proDialog != null) {
					proDialog.dismiss();
				}
				emptyTV.setText(e.getMessage());						
			}
		});
	}
	
	protected  boolean checkNet() {   
		ConnectivityManager cwjManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
		boolean b=cwjManager.getActiveNetworkInfo().isAvailable();
		return b;
 	} 
	
	
	protected void call(String mobile){		
		if(!ToolUtils.isPhone(mobile)){
			ToolUtils.toastText(CommonListActivity.this, "手机号码错误！");
		}else{					
			Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ mobile));				
			startActivity(intent);
		}						
		
		
	}
	
	protected void sms(String mobile){		
		if(!ToolUtils.isPhone(mobile)){
			ToolUtils.toastText(CommonListActivity.this, "手机号码错误！");
		}else{
			Uri smsToUri = Uri.parse("smsto:123456"); 
				 Intent sendIntent = new Intent(Intent.ACTION_VIEW, smsToUri); 
				 sendIntent.putExtra("address", mobile); 
				 sendIntent.setType("vnd.android-dir/mms-sms"); 
				 startActivity(sendIntent); 
		}		
	}
	
	
	protected ProgressDialog proDialog;
	protected void showProgressDialog(String title, String content,boolean bl1, boolean bl2) {
		proDialog = ProgressDialog.show(this, null, content, bl1, bl2);
	}		
	
	protected void showDialog(final Context context,String appName,final String programname) {   
        AlertDialog.Builder builder = new AlertDialog.Builder(context);   
//        builder.setIcon(R.drawable.icon);   
        builder.setTitle("提示");   
        builder.setMessage("没有安装"+appName+",是否下载?");   
        builder.setPositiveButton("确定",   
                new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int whichButton) {   
                    	if(Constants.LOGIN_NONET){
							Toast.makeText(context, "离线登录不提供安装下载",Toast.LENGTH_SHORT).show();	
						}else{
							if(!checkNet()){
								Toast.makeText(context,"网络异常，请重试",Toast.LENGTH_SHORT).show();
							}else{
								programName=programname;
								showProgressDialog(getString(R.string.down_title), getString(R.string.down_content), true, true);
								Thread loginThread = new Thread(new fileDownThread());
								loginThread.start();
							}
						}
                    }   
                });              
        builder.setNegativeButton("取消",   
                new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int whichButton) {                        
                    }   
                });   
        builder.show();   
    }   

	String programName="";	
	class fileDownThread implements Runnable {		 
		public void run() {
			HttpDownloader httpDownloader = new HttpDownloader();										
			int result = httpDownloader.downFileInSd(Constants.SERVER_URL+"/templateFile/"+programName, ReadProperties.getPropertyByStr("client.filepaht"), programName);
			Message msg = mainHandler.obtainMessage();								
			msg.arg1=result;
			mainHandler.sendMessage(msg);				
		}	
	}
	
	Handler mainHandler = new Handler() {
		public void handleMessage(Message msg) {
			proDialog.dismiss();
			if(msg.arg1==0){
				String filePath=Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+ ReadProperties.getPropertyByStr("client.filepaht") + File.separator + programName;
				Intent i = new Intent(Intent.ACTION_VIEW); 						   
				i.setDataAndType(Uri.fromFile(new File(filePath)),"application/vnd.android.package-archive"); 
				startActivity(i);
			}else if(msg.arg1==1){
				Toast.makeText(CommonListActivity.this, getResources().getString(R.string.no_sd),Toast.LENGTH_SHORT).show();
			}else if(msg.arg1==-1){
				Toast.makeText(CommonListActivity.this, "下载失败",Toast.LENGTH_SHORT).show();
			}			
		}
	};

	
	
	
	
	
	public SimpleAdapter getMenuAdapter(String[] menuNameArray,	int[] imageResourceArray) {
		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < menuNameArray.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("itemImage", imageResourceArray[i]);
			map.put("itemText", menuNameArray[i]);
			data.add(map);
		}
		SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
				R.layout.gridview_menu_item, new String[] { "itemImage","itemText" }, new int[] { R.id.gridview_item_iv,R.id.gridview_item_tv });
		return simperAdapter;
	}
	
	

}
